//
//  RHFbSelectDevCell.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2023/11/28.
//

#import "RHFbSelectDevCell.h"
#import "productCommentCollectionView.h"
#import "headImageSelectView.h"
#import "imagePickerController.h"
#import "RHTools.h"
#import "TZImagePickerController.h"
#import "showPictureView.h"
#import "RHDeviceListViewController.h"

@interface RHFbSelectDevCell()
@property (nonatomic ,strong) productCommentCollectionView *collectionView;

@property (nonatomic ,strong) QMUILabel *deviceIdLab;

@property (nonatomic ,strong) UIView *devBgView;

@property (nonatomic ,strong) QMUITextField *textField;

@end
@implementation RHFbSelectDevCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        [self setupUI];
        [self setActions];
    }
    return self;
}

-(void)setKeys:(NSArray *)keys
{
    [super setKeys:keys];
    kWeakSelf;
    //产品建议
    if([self.para[@"type"] isEqual:@(2)]){
        self.devBgView.hidden = YES;
        self.collectionView.hidden = YES;
        [self.textField mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weakSelf.devBgView.mas_bottom).offset(-50);
        }];
        
        [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.offset(-20);
        }];
    }else{
        self.devBgView.hidden = NO;
        self.collectionView.hidden = NO;
        [self.textField mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weakSelf.devBgView.mas_bottom).offset(15);
        }];
        
        [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.offset(100);
        }];
    }
}

-(void)setupUI{
    kWeakSelf;
    _photoArr = [NSMutableArray array];
    self.contentView.backgroundColor = CFTabViewBgColor;
    
    UIView *containView = kView;
    containView.backgroundColor = [UIColor whiteColor];
    [containView setMiddleBorder];
    [self.contentView addSubview:containView];
    [containView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.right.mas_offset(-15);
        make.top.bottom.mas_offset(0);
    }];
    
    UIView *bgView = kView;
    bgView.backgroundColor = kCOLOR_HEX(@"#F4F6F9");
    bgView.layer.cornerRadius = 8;
    bgView.layer.masksToBounds = YES;
    [containView addSubview:bgView];
    self.devBgView = bgView;
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.right.mas_offset(-15);
        make.top.mas_offset(5);
        make.height.offset(50);

    }];
    
    QMUILabel *deviceIdLab = [[QMUILabel alloc]init];
    deviceIdLab.text = @"请选择设备".rh_localized;
    deviceIdLab.font = [UIFont systemFontOfSize:14];
    deviceIdLab.textColor = [UIColor colorWithHexString:@"3F4443" alpha:0.8];
    [bgView addSubview:deviceIdLab];
    self.deviceIdLab = deviceIdLab;
    
    QMUIButton *selectBtn = [[QMUIButton alloc]init];
    [selectBtn setImage:[UIImage imageNamed:@"cell_arrow"] forState:UIControlStateNormal];
    selectBtn.imagePosition = QMUIButtonImagePositionRight;
    [selectBtn setTitle:@" " forState:UIControlStateNormal];
    selectBtn.spacingBetweenImageAndTitle = kUI_WINDOW_W - 60-45;
    [bgView addSubview:selectBtn];
    [selectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.height.offset(45);
        make.centerY.mas_offset(0);
    }];
    
    [deviceIdLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(12);
        make.right.mas_offset(-45);
        make.centerY.mas_offset(0);
    }];
    
    QMUITextField *textField = [[QMUITextField alloc]init];
    textField.placeholder = @"请输入问题标题".rh_localized;
    textField.layer.cornerRadius = 8;
    textField.layer.masksToBounds = YES;
    textField.backgroundColor = kCOLOR_HEX(@"#F4F6F9");
    textField.font = [UIFont systemFontOfSize:15];
    UIView *leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 10)];
    textField.leftView = leftView;
    textField.leftViewMode = UITextFieldViewModeAlways;
    textField.placeholderColor = [UIColor colorWithHexString:@"3F4443" alpha:0.4];
    [containView addSubview:textField];
    self.textField = textField;
    
    [textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.right.mas_offset(-15);
        make.height.offset(50);
        make.top.equalTo(bgView.mas_bottom).offset(15);
    }];
    
    QMUITextView *textView = [[QMUITextView alloc]init];
    textView.backgroundColor = kCOLOR_HEX(@"#F4F6F9");
    textView.font = [UIFont systemFontOfSize:15];
    textView.layer.cornerRadius = 8;
    textView.layer.masksToBounds = YES;
    textView.placeholder = @"请输入问题描述".rh_localized;
    textView.contentInset = UIEdgeInsetsMake(8, 8, 10, 8);
    textView.placeholderColor = [UIColor colorWithHexString:@"3F4443" alpha:0.4];
    textView.maximumTextLength = 1000;
    [containView addSubview:textView];
    
    [textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(textField);
        make.top.equalTo(textField.mas_bottom).offset(15);
        make.height.offset(120);
        
    }];
    
    QMUILabel *countLab = [[QMUILabel alloc]init];
    countLab.text = @"0/1000";
    countLab.font = [UIFont systemFontOfSize:14];
    countLab.textColor = [UIColor colorWithHexString:@"3F4443" alpha:0.8];
    [containView addSubview:countLab];
    [countLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(textView).offset(-10);
        make.bottom.equalTo(textView).offset(-10);
    }];
    
    
    //图片选择
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    productCommentCollectionView *pictureView = [[productCommentCollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
    [containView addSubview:pictureView];
    self.collectionView = pictureView;
    [pictureView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(textView);
        make.top.equalTo(textView.mas_bottom).offset(15);
        make.height.offset(100);
        make.bottom.mas_offset(-15);
    }];
    //选择设备
    [[selectBtn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
        RHDeviceListViewController *listVC = [[RHDeviceListViewController alloc]init];
        listVC.userId = [PrivateMange share].currentUser.userId;
        kWeakSelf;
        listVC.selectBlock = ^(NSString * _Nonnull deviceID) {
            weakSelf.deviceIdLab.text = deviceID;
            [weakSelf.para setObject:deviceID forKey:weakSelf.keys[0]];
        };
        [[RHTools getCurrentVC].navigationController pushViewController:listVC animated:YES];
    }];
    
    [[RACSignal merge:@[textField.rac_textSignal,RACObserve(textField, text)]]subscribeNext:^(NSString  *_Nullable text) {
        [weakSelf.para setObject:text forKey:weakSelf.keys[1]];
    }];
    
    [[RACSignal merge:@[textView.rac_textSignal,RACObserve(textView, text)]]subscribeNext:^(NSString  *_Nullable text) {
        countLab.text = [NSString stringWithFormat:@"%ld/1000",text.length];
        [weakSelf.para setObject:text forKey:weakSelf.keys[2]];
    }];
}



-(void)setActions{
    __weak typeof(self) weakSelf = self;
    //增加晒图
    self.collectionView.addPicture = ^(NSMutableArray * imgArr) {
       
        headImageSelectView * selectView = [[headImageSelectView alloc]initWithDataArr:@[Language(@"拍照"),Language(@"相册")]];
        [selectView show];
        selectView.didSelected = ^(NSInteger index) {
            //拍照
            if (index == 0) {
                imagePickerController * pickerVC = [[imagePickerController alloc] init];
                pickerVC.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
                // 设置类型
                pickerVC.sourceType = UIImagePickerControllerSourceTypeCamera;
                //pickerVC.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:pickerVC.sourceType];
                // 是否允许编辑
                pickerVC.allowsEditing = YES;
                [[RHTools getCurrentVC] presentViewController:pickerVC animated:YES completion:nil];
                pickerVC.didSelected = ^(UIImage *image) {
                    [imgArr insertObject:image atIndex:imgArr.count-1];
                    [weakSelf.collectionView reloadData];
                    
                    weakSelf.photoArr  = [NSMutableArray arrayWithArray:imgArr];
                    [weakSelf.photoArr removeLastObject];
                };
            }
            //相册
            else
            {
                NSLog(@"----->当前线程:%@",[NSThread currentThread]);
                TZImagePickerController * selectPhotoVC = [[TZImagePickerController alloc]initWithMaxImagesCount:4-imgArr.count delegate:weakSelf];
                selectPhotoVC.naviBgColor = [UIColor whiteColor];
                selectPhotoVC.naviTitleColor = [UIColor blackColor];
                selectPhotoVC.barItemTextColor = [UIColor blackColor];
                selectPhotoVC.modalPresentationStyle = UIModalPresentationFullScreen;
                [[RHTools getCurrentVC] presentViewController:selectPhotoVC animated:YES completion:nil];
                selectPhotoVC.didFinishPickingPhotosHandle = ^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
                    
                    [imgArr insertObjects:photos atIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(imgArr.count-1, photos.count)]];
                    [weakSelf.collectionView reloadData];
                    
                    weakSelf.photoArr  = [NSMutableArray arrayWithArray:imgArr];
                    [weakSelf.photoArr removeLastObject];
                };
            }
        };
    };
    //删除晒图
    self.collectionView.deletePicture = ^(NSMutableArray * imgArr, NSInteger index) {
        [imgArr removeObjectAtIndex:index];
        [weakSelf.collectionView reloadData];
        
        weakSelf.photoArr  = [NSMutableArray arrayWithArray:imgArr];
        [weakSelf.photoArr removeLastObject];
    };
    //查看图片
    self.collectionView.showPicture = ^(NSInteger index, NSMutableArray *imgArr, NSMutableArray * fromFrames) {
        NSMutableArray * images = [NSMutableArray arrayWithArray:imgArr];
        [images removeLastObject];
        [[[showPictureView alloc]init] showImages:images FromFrames:fromFrames index:index];
    };
}
@end
